package com.rc.evcharger.service.hlhtsk;


import com.rc.evcharger.mapper.hlhtsk.HlhtskTenantMapper;
import com.rc.evcharger.model.hlhtsk.HlhtskTenant;
import com.rc.evcharger.model.hlhtsk.HlhtskTenantExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class HlhtskTenantServiceImpl implements HlhtskTenantService {

    @Autowired
    private HlhtskTenantMapper hlhtskTenantMapper;

    @Override
    public HlhtskTenantMapper getMapper() {
        return hlhtskTenantMapper;
    }

    @Override
    public HlhtskTenant selectHlhtTenantByTenantCode(String tenantCode) {
        HlhtskTenantExample example = new HlhtskTenantExample();
        //SAAS内部自联或充其它运营商时已唯一的
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andStatusEqualTo("0")
                .andTenantHlhtTypeEqualTo("0");

        List<HlhtskTenant> hlhtTenants = hlhtskTenantMapper.selectByExample(example);
        if (hlhtTenants.size() > 0) {
            return hlhtTenants.get(0);
        } else {
            return null;
        }
    }

    @Override
    public HlhtskTenant selectBetweenTenantCode(String tenantCode, String hlhtTenantCode) {
        //tenantCode 互联运营商（入口方）
        //hlhtTenantCode 设备方

        HlhtskTenantExample example = new HlhtskTenantExample();
        //hlht_tenant_code:SAAS内互联互通时对方运营商（如果是自用，跟tenant_code一致）
        //example.createCriteria().andTenantCodeEqualTo(tenantCode)
        //        .andHlhtTenantCodeEqualTo(hlhtTenantCode)
        //        .andStatusEqualTo("0");

        //转换tenantCode和hlhtTenantCode
        example.createCriteria().andTenantCodeEqualTo(hlhtTenantCode)
                .andHlhtTenantCodeEqualTo(tenantCode)
                .andStatusEqualTo("0");

        List<HlhtskTenant> list = hlhtskTenantMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HlhtskTenant selectHlhtTenantCode(String tenantCode, String hlhtTenantCode) {
        //状态(0正常 1停用) 默认 0
        HlhtskTenantExample example = new HlhtskTenantExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtTenantCodeEqualTo(hlhtTenantCode).andStatusEqualTo("0");
        List<HlhtskTenant> list = hlhtskTenantMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
